﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Runtime.Serialization.Json;

namespace Utility.jqGrid
{
    /// <summary>
    /// 解析jqGrid查询条件
    /// </summary>
    public class jqGridJsonHelper
    {
        /// <summary>
        /// 获取jqGrid查询条件字符串
        /// </summary>
        /// <param name="strFilters"></param>
        /// <returns></returns>
        public static string jqGridFilte(string strFilters)
        {
            string searchCase = string.Empty;
            try
            {
                string conditions = HttpUtility.UrlDecode(strFilters);
                using (var mStream = new MemoryStream(Encoding.UTF8.GetBytes(conditions)))    //解决中文乱码问题
                {
                    DataContractJsonSerializer dcjson = new DataContractJsonSerializer(typeof(jqGridFilter));
                    jqGridFilter filters = (jqGridFilter)dcjson.ReadObject(mStream);
                    searchCase = jqGridFilte(filters);
                }
            }
            catch (Exception ex)
            {

            }
            return searchCase;
        }

        /// <summary>
        /// 解析jqGridFilter成查询条件
        /// </summary>
        /// <param name="filters">jqGridFilter</param>
        /// <returns>string查询条件</returns>
        private static string jqGridFilte(jqGridFilter filters)
        {
            string searchCase = string.Empty;
            if (filters.GroupOp.ToUpper() == "AND")
            {
                foreach (jqGridFilterRules rules in filters.JqGridFilterRulesList)
                {
                    if (searchCase.Length == 0)
                    {
                        searchCase = " " + jqGridFilte(rules);
                    }
                    else
                    {
                        searchCase += " and " + jqGridFilte(rules);
                    }
                }
            }
            else
            {
                foreach (jqGridFilterRules rules in filters.JqGridFilterRulesList)
                {
                    if (searchCase.Length == 0)
                    {
                        searchCase = " (" + jqGridFilte(rules);
                    }
                    else
                    {
                        searchCase += " or " + jqGridFilte(rules);
                    }
                }
                searchCase += ")";
            }
            return searchCase;
        }

        /// <summary>
        /// 解析jqGridFilterRules成查询条件
        /// </summary>
        /// <param name="model">jqGridFilterRules</param>
        /// <returns>string查询条件</returns>
        public static string jqGridFilte(jqGridFilterRules model)
        {
            string strWhere = string.Empty;
            string strSearchOper = model.Op;
            string strFieldData = model.Data.Replace("'", "");
            string strField = model.Field;
            switch (strSearchOper)
            {
                case "ni"://不属于
                    strWhere = string.Concat(strField, " not in ", "('" + strFieldData + "')");
                    break;
                case "in"://属于
                    strWhere = string.Concat(strField, " in ", "('" + strFieldData + "')");
                    break;
                case "bn"://不开始于
                    strWhere = string.Concat(strField, " not like ", "'" + strFieldData + "%'");
                    break;
                case "bw"://开始于
                    strWhere = string.Concat(strField, " like ", "'" + strFieldData + "%'");
                    break;
                case "eq"://等于
                    if (strFieldData.IsDateTime())
                    {
                        strWhere = string.Concat("CONVERT(varchar(100) , " + strField + ", 23 )", " = ", "'" + strFieldData + "'");
                    }
                    else
                    {
                        strWhere = string.Concat(strField, " = ", "'" + strFieldData + "'");
                    }
                    break;
                case "ne"://不等于
                    if (strFieldData.IsDateTime())
                    {
                        strWhere = string.Concat("CONVERT(varchar(100) , " + strField + ", 23 )", " != ", "'" + strFieldData + "'");
                    }
                    else
                    {
                        strWhere = string.Concat(strField, " != ", "'" + strFieldData + "'");
                    }
                    break;
                case "lt"://小于 
                    if (strFieldData.IsDateTime())
                    {
                        strWhere = string.Concat("CONVERT(varchar(100) , " + strField + ", 23 )", " < ", "'" + strFieldData + "'");
                    }
                    else
                    {
                        strWhere = string.Concat(strField, " < ", "'" + strFieldData + "'");
                    }
                    break;
                case "le"://小于等于
                    if (strFieldData.IsDateTime())
                    {
                        strWhere = string.Concat("CONVERT(varchar(100) , " + strField + ", 23 )", " <= ", "'" + strFieldData + "'");
                    }
                    else
                    {
                        strWhere = string.Concat(strField, " <= ", "'" + strFieldData + "'");
                    }
                    break;
                case "gt"://大于
                    if (strFieldData.IsDateTime())
                    {
                        strWhere = string.Concat("CONVERT(varchar(100) , " + strField + ", 23 )", " > ", "'" + strFieldData + "'");
                    }
                    else
                    {
                        strWhere = string.Concat(strField, " > ", "'" + strFieldData + "'");
                    }
                    break;
                case "ge"://大于等于
                    if (strFieldData.IsDateTime())
                    {
                        strWhere = string.Concat("CONVERT(varchar(100) , " + strField + ", 23 )", " >= ", "'" + strFieldData + "'");
                    }
                    else
                    {
                        strWhere = string.Concat(strField, " >= ", "'" + strFieldData + "'");
                    }
                    break;
                case "ew"://结束于
                    strWhere = string.Concat(strField, " like ", "'%" + strFieldData + "'");
                    break;
                case "en"://不结束于
                    strWhere = string.Concat(strField, " not like ", "'%" + strFieldData + "'");
                    break;
                case "cn"://包含
                    strWhere = string.Concat(strField, " like ", "'%" + strFieldData + "%'");
                    break;
                case "nc"://不包含 
                    strWhere = string.Concat(strField, " not like ", "'%" + strFieldData + "%'");
                    break;
                case "nu"://不存在
                    strWhere = string.Concat(strField, " not like ", "'%" + strFieldData + "%'");
                    break;
                case "nn"://存在 
                    strWhere = string.Concat(strField, " like ", "'%" + strFieldData + "%'");
                    break;
            }
            return strWhere;
        }
    }
}
